package org.ucas.kgraph.dto;

import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import static java.util.stream.Collectors.toList;

public class ListDto<E> extends ResponseDto {

  public long totalSize; // 总元素个数

  public List<E> entries; // 具体元素集合

  public ListDto(){
    super(HttpStatus.OK,null);
  }

  public static <E> ListDto<E> of(List<E> list) {
    ListDto<E> dto = new ListDto<E>();
    dto.totalSize = list==null?0:list.size();
    dto.entries = list==null?new ArrayList<E>():list;
    return dto;
  }

  public static <E, F> ListDto<E> of(List<F> list, Function<F, E> mapper) {
    ListDto<E> dto = new ListDto<E>();
    dto.totalSize = list==null?0:list.size();
    dto.entries = list==null?new ArrayList<E>():list.stream().map(mapper)
            .collect(toList());
    return dto;
  }

  public long getTotalSize() {
    return totalSize;
  }

  public void setTotalSize(long totalSize) {
    this.totalSize = totalSize;
  }

  public List<E> getEntries() {
    return entries;
  }

  public void setEntries(List<E> entries) {
    this.entries = entries;
  }
}
